<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <script>
      let tree = {
        val: 1,
        left: {
          val: 2,
          left: {
            val: 4,
          },
          right: {
            val: 5,
          },
        },
        right: {
          val: 3,
          left: {
            val: 6,
          },
          right: {
            val: 7,
          },
        },
      }
      var levelOrderBottom = function (root) {
        function levelOrder(root) {
          if (!root) return []
          let res = []
          let queue = [root]
          while (queue.length) {
            res.push([])
            let len = queue.length
            for (let i = 0; i < len; i++) {
              let node = queue.shift()
              res[res.length - 1].push(node.val)
              if (node.left) {
                queue.push(node.left)
              }
              if (node.right) {
                queue.push(node.right)
              }
            }
          }
          console.log(res)
        }
        levelOrder(root)
      }
      console.log(levelOrderBottom(tree))
    </script>
  </body>
</html>
